/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.poll.om.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.common.core.util.DateUtils;
import com.hopes.bizz.poll.common.constant.MonitorConstant;
import com.hopes.bizz.poll.common.constant.MonitorDataTypeEnum;
import com.hopes.bizz.poll.om.entity.PollAnalyStatisticTimeEntity;
import com.hopes.bizz.poll.om.mapper.PollAnalyStatisticTimeMapper;
import com.hopes.bizz.poll.om.mapper.PollRtDataHisMapper;
import com.hopes.bizz.poll.om.service.PollAnalyStatisticTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;

/**
 * 监测数据统计时间
 *
 * @author daijie
 * @date 2023-10-10 10:29:36
 */
@Service
public class PollAnalyStatisticTimeServiceImpl extends ServiceImpl<PollAnalyStatisticTimeMapper, PollAnalyStatisticTimeEntity> implements PollAnalyStatisticTimeService {

    @Autowired
    private PollRtDataHisMapper pollRtDataHisMapper;

    @Override
    public PollAnalyStatisticTimeEntity getByDataType(MonitorDataTypeEnum monitorDataTypeEnum) {
        return this.getBaseMapper().getByDataType(monitorDataTypeEnum.name());
    }

    @Override
    public void createDayStatisticTask() {
        Date time = pollRtDataHisMapper.getMinimumDataTime();
        if (time == null) {
            time = new Date();
        }
         time = DateUtils.getDateStart(DateUtils.add(time, Calendar.DAY_OF_YEAR, MonitorConstant.ONE));
         PollAnalyStatisticTimeEntity entity = new PollAnalyStatisticTimeEntity();
        entity.setAnalyTime(time);
        entity.setAnalyType(MonitorDataTypeEnum.day.name());
        this.save(entity);
    }
}